package com.z_talk.client.core;

import com.z_talk.client.localization.TalkLocalzation;
import com.z_talk.client.view.PrivateTalkView;
import com.z_talk.client.view.TalkView;
import com.z_talk.core.enums.ResponseCodeEnum;
import com.z_talk.core.model.UserInfo;
import com.z_talk.core.util.TipsView;
import io.netty.channel.Channel;

/**
 * 服务器返回信息处理类
 */
public class MessageHandler {
    static TalkView talkView;

    public static void regiterHandler(Channel channel, ResponseCodeEnum message,String registerMsg){
        if (ResponseCodeEnum.Y.equals(message)){
            TipsView.notice(registerMsg,"注册通知");
        }else if (ResponseCodeEnum.N.equals(message)){
            TipsView.notice("注册失败","注册通知");
        }else {
            TipsView.warning("未知消息","注册通知");
        }
    }


    public static void loginHandler(Channel channel, ResponseCodeEnum loginResponse, UserInfo userInfo) {
        if (ResponseCodeEnum.Y.equals(loginResponse)){
            TipsView.notice("登录成功","登录通知");
            talkView = new TalkView(userInfo);
        }else if (ResponseCodeEnum.N.equals(loginResponse)){
            TipsView.notice("登录失败","登录通知");
            ClientMain.init();
        }else {
            ClientMain.init();
        }
    }

    public static void worldTalkHandler(Channel channel, String worldTalkResponse) {
        TalkLocalzation.saveWorldTalk(talkView.getUserInfo(),worldTalkResponse);
        talkView.updateMessage(worldTalkResponse);
    }

    public static void onlineHandler(Channel channel, String onlineResponse) {
        talkView.updateMessage(onlineResponse);
    }

    public static void onlineListHandler(Channel channel, UserInfo[] userInfos) {
        talkView.updateOnlineList(userInfos);
    }

    public static void noticeHandler(Channel channel, String notice) {
        TipsView.notice(notice,"系统公告");
    }

    public static void privateTalkHandler(Channel channel, UserInfo userInfo, String privateTalkResponse) {
        PrivateTalkView privateTalkView;
        if ((privateTalkView = talkView.privateTalkViewMap.get(userInfo.getUserId()))==null){
            talkView.privateTalkViewMap.put(userInfo.getUserId(),privateTalkView = new PrivateTalkView(talkView.getUserInfo(),userInfo));
        }
        privateTalkView.updateMessage(privateTalkResponse);
    }
}
